<?php


namespace app\controller\admin;


use app\BaseController;
use app\common\Consts;
use app\common\Domain;
use app\common\Ios;
use app\common\OrderStatus;
use app\common\Service;
use think\facade\Db;

class DomainController extends BaseController
{
    public function getList()
    {
        try {
            $where = [];
            $param = $this->request->param();
            $array = Domain::getArr();
            $arrayKey = array_keys($array);
            foreach ($param as $k => $v) {
                if (!in_array($k, $arrayKey)) {
                    continue;
                }
                if (in_array($k, array_keys(Consts::getArr()))) {
                    $prefix = 'b.';
                } else {
                    $prefix = 'a.';
                }
                $fieldType = $array[$k];
                if ($fieldType == 'int') {
                    $where[] = [$prefix . $k, '=', $v];
                } elseif ($fieldType == 'string') {
                    if($k == 'username'){
                        $where[] = [$prefix . 'user_name', 'like', '%' . $v . '%'];
                    }else{
                        $where[] = [$prefix . $k, 'like', '%' . $v . '%'];
                    }

                } elseif ($fieldType == 'time') {
                    $where[] = [$prefix . $k, 'between', [strtotime($v[0]), strtotime($v[1])]];
                } elseif ($fieldType == 'array') {
                    $where[] = [$prefix . $k, 'in', $v];
                } else {
                    $where[] = ['b.program_sn|b.user_name|b.program_name', 'like', '%' . $v . '%'];
                }
            }
            $orderBy = $this->request->param('order_by', 'id', 'trim');
            if (in_array($orderBy, array_keys(Consts::getArr()))) {
                $orderBy = 'b.'.$orderBy;
            } else {
                if($orderBy == 'process_name'){
                    $orderBy = 'b.process';
                }else{
                    $orderBy = 'a.'.$orderBy;
                }
            }
            $sort = $this->request->param('sort', 'desc', 'trim');
            $list = Db::name('domain')
                ->alias('a')
                ->field('a.*,b.program_sn,b.user_name,b.program_name,b.process')
                ->leftJoin('user b', 'b.id = a.user_id')
                ->where($where)
                ->whereNotNull('b.id')
                ->order($orderBy, $sort)
                ->paginate(['list_rows' => $this->limit, 'page' => $this->page])
                ->toArray();
            foreach ($list['data'] as &$v) {
                $v['start_time'] = $v['start_time'] > 0 ? date('Y-m-d', $v['start_time']) : '';
                $v['end_time'] = $v['end_time'] > 0 ? date('Y-m-d', $v['end_time']) : '';
                $v['create_time'] = $v['create_time'] > 0 ? date('Y-m-d', $v['create_time']) : '';
                $v['process_name'] = OrderStatus::getProcess($v['process']);
            }
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    public function edit()
    {
        Db::startTrans();
        try {
            $data = $this->request->param();
            $id = $data['id'];
            if (!$id) {
                throw new \Exception('非法请求');
            }
            $info = Db::name('domain')->where('id', $id)->find();
            if (!$info) {
                throw new \Exception('信息不存在');
            }
            $process = $data['process'];

            $data['start_time'] = strtotime($data['start_time']);
            $data['end_time'] = strtotime($data['end_time']);
            $data['update_time'] = time();

            unset($data['program_sn'], $data['user_name'], $data['program_name'], $data['id'], $data['process'],
                $data['process_name'],$data['create_time'],$data['status']);


            Db::name('domain')->where('id', $id)->update($data);

            $userInfo = Db::name('user')->where('id', $data['user_id'])->find();
            if ($userInfo['process'] != $process) {
                Db::name('user')->where('id', $data['user_id'])->update([
                    'process' => $process,
                ]);
                Db::name('user_record')->insert([
                    'user_id' => $data['user_id'],
                    'write_user_id' => $this->request->userId,
                    'context' => '修改 项目进度：' . OrderStatus::getProcess($userInfo['process']) . '，改为：' . OrderStatus::getProcess($process),
                    'create_time' => getFormatDate()
                ]);
            }

            Db::commit();
            return $this->success();
        } catch (\Exception $exception) {
            Db::rollback();
            return $this->error($exception->getMessage());
        }
    }

    public function del()
    {
        Db::startTrans();
        try {
            $id = $this->request->param('id');
            if ($id <= 0) {
                throw new \Exception('非法请求');
            }
            $info = Db::name('domain')->where('id', $id)->find();
            if (!$info) {
                throw new \Exception('信息不存在');
            }

            Db::name('domain')->where('id', $id)->delete();
            Db::commit();
            return $this->success();
        } catch (\Exception $exception) {
            Db::rollback();
            return $this->error($exception->getMessage());
        }
    }
}